package com.irdstudio.bhcredit.core.batch.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.irdstudio.bhcredit.core.batch.BatchConstant;

/**
 * 数据表操作类-[表名: 02.批次任务实例(bat_inst_task)]
 * 通过JDBC实现基本的数据表操作(CRUD) 
 * @author 代码自动生成
 * @version 1.0
 * @date 2014-05-10
 */
public class BatInstTaskDao {

	/* 连接对象 */
	Connection conn = null;	

	public BatInstTaskDao(Connection conn){
		this.conn = conn;
	}

	/**
	 * 根据主键查询单条记录
	 * @param 
	 * @return
	 * @throws SQLException
	 */
	public BatInstTask queryWithKeys(String batchId,int batchOrder,String taskId)
			throws SQLException {
		BatInstTask dc = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM bat_inst_task WHERE batch_id=? and batch_order=? and task_id=?");
			ps.setString(1,batchId);
			ps.setInt(2,batchOrder);
			ps.setString(3,taskId);
			rs = ps.executeQuery();
			if (rs.next()) {
				dc = new BatInstTask();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchOrder(rs.getInt("batch_order"));
				dc.setTaskId(rs.getString("task_id"));
				dc.setBatchDate(rs.getString("batch_date"));
				dc.setTaskName(rs.getString("task_name"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setStageName(rs.getString("stage_name"));
				dc.setTaskRunState(rs.getString("task_run_state"));
				dc.setTaskInterveneState(rs.getString("task_intervene_state"));
				dc.setStartTime(rs.getString("start_time"));
				dc.setEndTime(rs.getString("end_time"));
				dc.setCostTime(rs.getBigDecimal("cost_time"));
				dc.setWarnCount(rs.getInt("warn_count"));
				dc.setOther(rs.getString("other"));
				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
				dc.setStateDesc(rs.getString("state_desc"));
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatInstTaskWithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dc;
	}

	/**
	 * 根据查询条件查询多条或单条记录
	 * @param szCondition 
	 * @return
	 * @throws SQLException
	 */
	public List<BatInstTask> queryWithCond(String szCondition,String szOrderBy)
			throws SQLException {
		List<BatInstTask> dcList = new ArrayList<BatInstTask>();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement("SELECT * FROM bat_inst_task "
					+ szCondition + " " + szOrderBy);
			rs = ps.executeQuery();
			while (rs.next()) {
				BatInstTask dc = new BatInstTask();
				dc.setBatchId(rs.getString("batch_id"));
				dc.setBatchOrder(rs.getInt("batch_order"));
				dc.setTaskId(rs.getString("task_id"));
				dc.setBatchDate(rs.getString("batch_date"));
				dc.setTaskName(rs.getString("task_name"));
				dc.setStageId(rs.getString("stage_id"));
				dc.setStageName(rs.getString("stage_name"));
				dc.setTaskRunState(rs.getString("task_run_state"));
				dc.setTaskInterveneState(rs.getString("task_intervene_state"));
				dc.setStartTime(rs.getString("start_time"));
				dc.setEndTime(rs.getString("end_time"));
				dc.setCostTime(rs.getBigDecimal("cost_time"));
				dc.setWarnCount(rs.getInt("warn_count"));
				dc.setOther(rs.getString("other"));
				dc.setBatchSerialNo(rs.getString("batch_serial_no"));
				dc.setStateDesc(rs.getString("state_desc"));
				dcList.add(dc);
			}
		} catch (SQLException e) {
			throw new SQLException("queryBatInstTaskWithCond is Wrong!"
					+ e.getMessage());
		} finally {
			close(rs, null, ps);
		}
		return dcList;
	}
	
	/**
	 * 根据条件修改
	 * @param condition
	 * @throws SQLException
	 */
	public boolean updateBatInstTask(String szCondition)throws SQLException{
		PreparedStatement ps = null;
		boolean executeResult = false;
		try {
			ps = conn.prepareStatement("update bat_inst_task "
					+ szCondition );
			executeResult = ps.execute();
		} catch (SQLException e) {
			throw new SQLException("updateBatInstTask is Wrong!"
					+ e.getMessage());
		} finally {
			close(null, null, ps);
		}
		
		return executeResult;
	}		

	/**
	 * 关闭资源
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 重置失败的任务
	 * @param batchId
	 * @throws SQLException 
	 */
	public void resetFaildTask(String batchId) throws SQLException {
		PreparedStatement ps = null;
		try {
			ps = conn
					.prepareStatement("update bat_inst_task set task_run_state=?,start_time='',end_time='',cost_time=0 where batch_id=? and task_run_state='7'");
			ps.setString(1, String.valueOf(BatchConstant.TASK_STATE_WTRUN));
			ps.setString(2, batchId);
			ps.execute();
		} catch (SQLException e) {
			throw e;
		} finally {
			close(null, null, ps);
		}
	}
}
